package com.freud.logic.authorize.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.freud.framework.base.BaseController;
import com.freud.framework.base.BaseRequest;
import com.freud.logic.authorize.beans.User;
import com.freud.logic.authorize.service.AuthorizeService;

@RequestMapping("/manager/user")
@Controller
public class UserController extends BaseController {

	@Autowired
	private AuthorizeService authorizeService;

	@RequestMapping(value = "/index")
	public String index() {
		return "manager.user.index";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(BaseRequest baseRequest) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", authorizeService.getAllUsers(buildRowBounds()).size());
		result.put("rows", authorizeService.getAllUsers(buildRowBounds(baseRequest)));
		return result;
	}

	@RequestMapping(value = "/edit")
	public ModelAndView edit(int id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("manager.user.edit");
		mav.addObject("user", authorizeService.getUserById(id));
		return mav;
	}

	@RequestMapping(value = "/update")
	public String update(User user) {
		authorizeService.update(user);
		return "redirect:/manager/user/index";
	}

	@RequestMapping(value = "/delete")
	public String delUser(int id) {
		authorizeService.deleteUser(id);
		return "redirect:/manager/user/index";
	}

	@RequestMapping(value = "/question")
	public String question(int id) {
		User user = authorizeService.getUserById(id);
		authorizeService.update(user);
		return "redirect:/manager/user/index";
	}

	@RequestMapping(value = "/answer")
	public String answer(int id) {
		User user = authorizeService.getUserById(id);
		authorizeService.update(user);
		return "redirect:/manager/user/index";
	}

}
